#!/usr/bin/perl -w

# 如何保证中断后不重复删人
# 如何保证断网后不退出
# 如何保证爬虫一直在线
# 打印的地方加序号

use strict;
use SinaBot;
use SinaDB;
use Tools;
use XML::Simple;
use Data::Dumper;


sub readxml{
    my($name) = @_;
    my $config = XMLin('xmlconfig');
    return $$config{$name};
}
sub writexml{
    my($name,$value) = @_;
    
    my $config = XMLin('xmlconfig');
    $$config{$name} = $value;
    open(XMLFILE, ">xmlconfig");
    print XMLFILE  XMLout($config);
    close(XMLFILE);
}

#&writexml('xxx',2);
#print &readxml('xxx');
####################### MAIN #########################

our $db = SinaDB->do_new;
our $bot = SinaBot->do_new;
my $dbname = 'sinadb';
my $dbuser = 'sinabot';
my $dbhost = '192.168.1.7';
my $dbpasswd = 'sinabot123';

if(not $ARGV[0]){
    print "command:"; chomp ($ARGV[0]=<STDIN>);
}
if($ARGV[0] eq 'grab'){
	&grab_t_sina; #抓取帐号(名博客粉丝)
}elsif($ARGV[0] eq 'grab2'){
	&grab2_t_sina($ARGV[1]); #抓取帐号(最近发帖)
}elsif($ARGV[0] eq 'check_user'){
	&check_users($ARGV[1]);
}elsif($ARGV[0] eq 'test_vpn'){
	&test_vpn($ARGV[1]);
}elsif($ARGV[0] eq 'follow'){
    &follow_t_sina;
}elsif($ARGV[0] eq 'txt'){
	$db->temp_get_bj;
}elsif($ARGV[0] eq 'auto_blog'){

}elsif($ARGV[0] eq 't'){
    my $email = 'coxoto@gmail.com';
    my $password = '860730';
    $bot->do_login($email,$password);
    $bot->new_blog("中文的当然也可以","6d670b7bjw6dcddj7il2wj");

    #$db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    #eval{
        #my $ref = {'a'=>'aaa'};
        #$ref='';
        #sayinf $$ref{'a'};
    #};if($@){sayerr $@;}
    #sayinf 'passby';
}else{
	sayerr "Please check ur input!!";
}


#===================== check user ================================
sub check_users{
    my ($start) = @_;
    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    my $email = 'coxoro@gmail.com';
    my $password = '860730';
    $bot->do_login($email,$password);
	while (1){
		my $_userid = $db->get_user_need_check($start);
		if ($_userid){
			my $ref = $bot->check_user($_userid);
			print "area: ".$$ref{'area'}." \n";
			print "sex: ".$$ref{'sex'}." \n";
			print "name: ".$$ref{'name'}."\n";
			print "fansnum: ".$$ref{'fans_num'}."\n";
			$db->set_user_info($_userid, $ref);
		}else{
			sleep(600);
		}
	}
}

#===================== grab ==================================
sub grab_t_sina{
    #my ($db) = @_;
    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    my $email = 'coxoro@gmail.com';
    my $password = '860730';
    $bot->do_login($email,$password);

    while(1){
        my $_host = $db->get_host4grab;
        $bot->do_start_grep("fans",$$_host{'id'});

        my @_user_ids;
        my $_grab_last_num = 0;
        my $_one_host_end = 0;
        my @_grab_last_ids_arr = ();
        

        while(@_user_ids=$bot->do_get_onepage){

            foreach (@_user_ids) {
                my $_id = $_;

                if( index($$_host{'grab_last_ids'}, $_id) > -1){
                    $_one_host_end = 1;
                    sayinf "this site has no new fans";
                    last;
                }
                sayinf "[$_id]";

                $_grab_last_num ++;
                $db->insert_user($_id);
                if ( $bot->get_if_first_grab ) {
                    push (@_grab_last_ids_arr , $_id); 
                }
            }
	        sleep(1);
            if($_one_host_end){last;}
        }

        my @_old_grab_ids = split /,/, $$_host{'grab_last_ids'};
        while($#_grab_last_ids_arr < 5){
            push ( @_grab_last_ids_arr ,(shift @_old_grab_ids));
        }
        my $_grab_last_ids = join ",", @_grab_last_ids_arr;
        $db->update_host_after_grab($$_host{'id'},$_grab_last_ids,$_grab_last_num);
	    sleep(2);
    }    
}

#===================== grab2 ==================================
sub grab2_t_sina{
    my ($word) = @_; #抓取最新发表某些博客的人

    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    my $email = 'coxoro@gmail.com';
    my $password = '860730';
    $bot->do_login($email,$password);
    my @_user_refs;
    my $_first_one='';

    while(1){
        if(not $word){$word='购'};
        my @_user_ids=$bot->do_get_onepage_search($word);
        my $_first_one_temp='';
        foreach (@_user_ids) {
            my $_id = $_;
	    #记录搜索记录第一条的id
            if( not $_first_one_temp ){
                $_first_one_temp = $_id;
            }
	    #如果抓到一条id等于上一次的首条id 
	    #说明下面的是重复的 跳出循环 
            if( $_id eq $_first_one ){sayinf "no new blog user!";last;}

            sayinf "[$_id]";
            $db->insert_user($_id);
        }
        $_first_one = $_first_one_temp;
	sleep(30);
    }
}

#======================= unfollow ===============================
sub unfollow_t_sina{
    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    my $max_unfollow_num = 245;
    my $_vpn_num=19;

    while ( my $_myhost4unfollow_ref = $db->get_myhost4unfollow )
    {
        sayinf "Begin to unfollow".$$_myhost4unfollow_ref{'email'}." " 
                .$$_myhost4unfollow_ref{'password'}; 

        &change_next_vpn();

        $bot->do_login($$_myhost4unfollow_ref{'email'},
            $$_myhost4unfollow_ref{'password'});
        $bot->set_inf($$_myhost4unfollow_ref{'id'});

        $bot->do_start_grep('follow');
        $bot->set_reverse_order;

        my @_user_refs;
        my $_unfollow_num = 0;
        my $_one_host_end = 0;

        while(@_user_refs=($bot->do_get_onepage)){

            foreach (reverse @_user_refs) {
                my %_temp = %$_;
                &printoutuser(\%_temp);
                if( $bot->unfollow($_temp{'id'}) ){
                    #sleep(1);
                    $_unfollow_num ++;
                    $db->update_follow($_temp{'id'},$$_myhost4unfollow_ref{'id'},0,2);
                }
                if( $_unfollow_num >= $max_unfollow_num ){
                    $_one_host_end = 1;
                    last;
                }
            }
            if($_one_host_end){last;}
        }
        $db->update_host_after_unfollow( $$_myhost4unfollow_ref{'id'},
            $_unfollow_num);
        
    }
}


sub unfollow_one_host{
    my ($max_unfollow_num,$max_error_num) = @_;
    $bot->do_start_grep('follow');
    $bot->set_reverse_order;

    my @_user_refs;
    my $_unfollow_num = 0;
    my $_one_host_end = 0;
    my $_error_num = 0;
    while(@_user_refs=($bot->do_get_onepage)){
        foreach (reverse @_user_refs) {
            my $_id = $_;
            my $_unfollow_result = $bot->unfollow($_id);
            if( $_unfollow_result ){
                if ($_unfollow_result == 5){
                    $_one_host_end = 1;last;
                }else{
                    $_error_num ++;
                    if ($_error_num > $max_error_num){ $_one_host_end = 1;last;}
                }
            }else{
                $_unfollow_num ++;
            }
            if( $_unfollow_num > $max_unfollow_num ){
                $_one_host_end = 1;
                last;
            }
        }
        if($_one_host_end){last;}
    }
}
#============================ follow =========================================
sub follow_t_sina{
    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
    my $_myhost_ref;
    my $_max_follow_counts = 240;
    my $_max_unfollow_counts = 250;
    my $_max_vpn_counts = 3;
    my $_max_unknown_error_counts = 50;
    #get a host of mine to follow
    while ($_myhost_ref = $db->select_one("myhosts","name is not null and area is not null and datediff(now(),gtime)>0")){
        &change_next_vpn;

        $bot->do_login($$_myhost_ref{'email'},$$_myhost_ref{'password'});
        $bot->set_inf($$_myhost_ref{'id'});
        
        &grab_self($_myhost_ref,'fans');
        &grab_self($_myhost_ref,'follow');

        &unfollow_one_host($_max_unfollow_counts,$_max_unknown_error_counts);

        my $_follow_counts = 0;
        my $_vpn_counts = 0;
        my $_user_ref;
        my $_follow_result;
        my $_fresult = "";
        my $_unkown_error_counts = 0;
        while(1){
            $_user_ref = $db->select_one("users","vip_verify=0 and over not like '%".$$_myhost_ref{'id'}."%' and area like '%".$$_myhost_ref{'area'}."%' and org_blog_num>0 and fans_num>0");
            if(not $_user_ref) {$_fresult = "no_man";last;}
            $_follow_result = $bot->follow($$_user_ref{'id'});
            if($_follow_result){
                #1验证 5超500 6超2000 9未知
                if($_follow_result == 1){
                    &change_next_vpn;
                    $_vpn_counts ++;
                    if ($_vpn_counts > $_max_vpn_counts){
                        $_fresult="vpn"; last;
                    } 
                }elsif($_follow_result == 4){
                    $_fresult="acount_err";last;
                }elsif($_follow_result == 5){
                    $_fresult = "500_op";
                    last;
                }elsif($_follow_result == 6){
                    $_fresult = "2000_full";
                    last;
                }elsif($_follow_result == 9){
                    $_unkown_error_counts ++;
                    sayinf "unknown error: user:".$$_user_ref{'id'}." host:".$$_myhost_ref{'id'};
                    if($_unkown_error_counts > $_max_unknown_error_counts){ $_fresult="maxerr";last; }
                }
            }else{
                $db->update("users",$$_user_ref{'id'},"over='".&addid($$_user_ref{'over'},$$_myhost_ref{'id'})."'");
                $_follow_counts++;
            }
            if ($_follow_counts > $_max_follow_counts){ last;}
        }
        $db->update("myhosts",$$_myhost_ref{'id'},"fresult='".$_fresult."',fcount='".$_follow_counts."',gtime = now()")
    }
    &close_vpn;
}

sub addid{
    my($ids,$id) = @_;
    if(not $ids){
        $ids = $id;
    }elsif(index($ids, $id) > -1){
    }else{
        $ids .= ",$id";
    }
    return $ids;
}

sub grab_self{
    my ($hostref,$type) = @_;
    my @_user_refs;
    my $_one_host_end = 0;
    my @_grab_last_ids_arr = ();
    $bot->do_start_grep($type);
    while(@_user_refs=$bot->do_get_onepage){
        foreach (@_user_refs) {
            my $_id = $_;
            my $_gids;
            if( index($$hostref{$type.'_gids'}, $_id) > -1){
                $_one_host_end = 1;
                last;
            }

            $db->insert_user($_id);
            my $_ref = $db->select_one("users","id=".$_id);
            if( $$_ref{'vip_verify'} == -1 ){
                eval{
			        $db->set_user_info($_id,$bot->check_user($_id));
                };if($@){
                    sayerr $@;
                    $db = SinaDB->do_new;
                    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
                }
            }
            $db->do("update users set over='".&addid($$_ref{'over'},$$hostref{'id'})."' 
                            where id=".$_id." and area like '%".$$hostref{'area'}."%'");

            if ( $bot->get_if_first_grab ) {
                push (@_grab_last_ids_arr , $_id); 
            }
        }
        if($_one_host_end){last;}
    }
    my @_old_grab_ids = split /,/, $$hostref{$type.'_gids'};
    while($#_grab_last_ids_arr < 5){
        push ( @_grab_last_ids_arr ,(shift @_old_grab_ids));
    }
    my $_grab_last_ids = join ",", @_grab_last_ids_arr;
    $db->update("myhosts",$$hostref{'id'},$type."_gids = '$_grab_last_ids'");
}

sub change_next_vpn{
    my $current_vpn = &readxml('current_vpn');
    &close_vpn;
    $current_vpn ++;
    if($current_vpn > 121) {$current_vpn = 1;}
    &writexml('current_vpn',$current_vpn);
    if(&change_vpn($current_vpn)){
        return &change_next_vpn;
    }else{
        return $current_vpn;
    }
}
sub test_vpn{
    my ($arg)=@_;
    foreach (1..150){ 
        sayinf &change_next_vpn($_);
    }
}
sub change_vpn{
    my ($num) = @_;
    my $_bot = SinaBot->do_new;
    my %vpn = &get_vpn_by_num($num);
    system("pptpsetup --create myvpn --server ".$vpn{'ip'}." --username coxoto --password 860730");
    system("cp /etc/ppp/chap-secrets-short /etc/ppp/chap-secrets");
    sleep 2;
    #print "..";
    system("pon myvpn");
    sleep 12;
    #print "..";
    if (system("route add -net 0.0.0.0 dev ppp0 > /dev/null")){
        return 1;
    }
    #print "..";
    sleep 2;
    if ($vpn{'ip'} eq $_bot->get_myip){
        sayinf "change vpn $num success!";
        return 0;
    }else{
        return 1;
    }
}
sub close_vpn{
    system("poff -a");
}
sub get_vpn_by_num{
    my ($vpn_num)=@_;

    my $_mstr = '\S*?\[(\d+)\]\S*?(\d+.\d+.\d+.\d+)';
    my %vpn=();
    my $line;
    open(FILENUM,"vpn.txt");
    foreach (1..$vpn_num){
        $line = <FILENUM>;
        chomp $line;
    }
    if($line =~ m{$_mstr}six){
        $vpn{'id'}=$1+0;
        $vpn{'ip'}=$2;
    }
    close(FILENUM);
    return %vpn;
}

sub temp_get_bj{
	my ($x) = @_;

    $db->connectdb($dbname,$dbhost,$dbuser,$dbpasswd);
	my $_sqr;
	$_sqr = $db->prepare("select id,name from users where area like '北京%' and vip_verify=0 and org_blog_num>1 and fans_num>2");
	$_sqr->execute();
	my $_ref;
	my $_line=0;
	while ( $_ref = $_sqr->fetchrow_hashref()){
		$$_ref{'name'} =~ s/(^")|("$)//g;
		print '@'.$$_ref{'name'}." ";         
		$_line++;
		if( $_line%10==0){ print "\n";}
	}
}


1;
